package com.sskj.gocoin.v.activity;

import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.sskj.gocoin.R;
import com.sskj.gocoin.R2;
import com.sskj.gocoin.common.App;
import com.sskj.gocoin.p.activity.RegisterPresenter;
import com.sskj.gocoin.utils.CheckUtil;
import com.sskj.gocoin.utils.ClickUtil;
import com.sskj.gocoin.utils.ToastUtil;
import com.sskj.gocoin.v.base.BaseActivity;
import com.sskj.lib.RConfig;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

/**
 * <pre>
 *     author : wk_math
 *     e-mail : wk_math@163.com
 *     time   : 2018/04/02
 *     desc   :
 *     version: 1.0
 * </pre>
 */

public class RegisterActivity extends BaseActivity<RegisterPresenter> {
    @BindView(R2.id.tv_goto_login)
    TextView tvGotoLogin;
    @BindView(R2.id.et_phone)
    EditText etPhone;
    @BindView(R2.id.iv_delete)
    ImageView ivDelete;
    @BindView(R2.id.bt_next_step)
    Button btNextStep;
    @BindView(R2.id.reigster_get_auth_code)
    TextView mRegisterGetAuthCode;
    @BindView(R2.id.register_input_auth_code)
    EditText mRegisterInputAuthCode;
    @BindView(R2.id.et_pwd)
    EditText mEtPwd;
    @BindView(R2.id.iv_toggle_pwd)
    ImageView mIvTogglePwd;
    @BindView(R2.id.et_pwd_again)
    EditText mEtPwdAgain;
    @BindView(R2.id.iv_toggle_pwd_again)
    ImageView mIvTogglePwdAgain;
    @BindView(R2.id.et_invite_code)
    EditText mEtInviteCode;
    @BindView(R2.id.register_user_agreement)
    TextView mRegisterUserAgreement;
    @BindView(R2.id.check_register_user_agreement)
    CheckBox mCheckRegisterUserAgreement;
    private DisposableSubscriber<Long> disposableSubscriber;
    private boolean isSee = false;
    private boolean isSeeAgain = false;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_register;
    }

    @Override
    public RegisterPresenter getPresenter() {
        return new RegisterPresenter();
    }

    @Override
    protected void onViewBinding() {

        ClickUtil.click(mRegisterUserAgreement, () -> WebViewActivity.start(mActivity, "reg_agree"));

        ClickUtil.click(tvGotoLogin, () -> {
            ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
            finish();
        });
        ClickUtil.click(ivDelete, () -> {
            etPhone.setText("");
        });

        ClickUtil.click(mRegisterGetAuthCode, () -> {//获取验证码
            if (CheckUtil.isMobile(etPhone.getText().toString())) {
                mPresenter.sendCode(etPhone.getText().toString());
            } else {
                if (CheckUtil.isEmail(etPhone.getText().toString())) {
                    mPresenter.sendEmail(etPhone.getText().toString());
                } else {
                    ToastUtil.showShort(App.INSTANCE.getString(R.string.strRegisterActivity203));
                    etPhone.setText("");
                }
            }
        });
        ClickUtil.click(btNextStep, () -> {//下一步
            if (!mCheckRegisterUserAgreement.isChecked()) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strRegisterActivity205));
                return;
            }
            String mobile = etPhone.getText().toString().trim();
            String pwd = mEtPwd.getText().toString().trim();
            String pwdAgain = mEtPwdAgain.getText().toString().trim();
            String authCode = mRegisterInputAuthCode.getText().toString().trim();
            if (!CheckUtil.isMobile(mobile) && !CheckUtil.isEmail(etPhone.getText().toString())) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strRegisterActivity206));
                return;
            }
            if (TextUtils.isEmpty(authCode)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strRegisterActivity207));
                return;
            }
            if (!CheckUtil.isPayPwd(pwd)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strRegisterActivity208));
                return;
            }
            if (!pwd.equals(pwdAgain)) {
                ToastUtil.showShort(App.INSTANCE.getString(R.string.strRegisterActivity209));
                return;
            }
            mPresenter.register(mobile, pwd, pwd, mEtInviteCode.getText().toString().trim(), authCode);

        });

        ClickUtil.click(200, mIvTogglePwd, () -> {
            if (isSee) {
                isSee = false;
                mEtPwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mIvTogglePwd.setImageResource(R.mipmap.unsee);
            } else {
                isSee = true;
                mEtPwd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mIvTogglePwd.setImageResource(R.mipmap.see);
            }
        });
        ClickUtil.click(200, mIvTogglePwdAgain, () -> {
            if (isSeeAgain) {
                isSeeAgain = false;
                mEtPwdAgain.setTransformationMethod(PasswordTransformationMethod.getInstance());
                mIvTogglePwdAgain.setImageResource(R.mipmap.unsee);
            } else {
                isSeeAgain = true;
                mEtPwdAgain.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                mIvTogglePwdAgain.setImageResource(R.mipmap.see);
            }
        });
    }

    public void sendCodeOk(String account) {
        startCount();
    }

    public void startCount() {
        mRegisterGetAuthCode.setEnabled(false);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                mRegisterGetAuthCode.setText(String.format(App.INSTANCE.getString(R.string.strRegisterActivity210), time));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                mRegisterGetAuthCode.setText(App.INSTANCE.getString(R.string.strRegisterActivity211));
                mRegisterGetAuthCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    public void registerSuccess() {
        ARouter.getInstance().build(RConfig.APP_LOGIN).navigation();
        finish();
    }
}
